# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library", "rust_proc_macro", "rust_test")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")

rust_library(
    name = "pw_log",
    srcs = [
        "pw_log.rs",
    ],
    crate_features = select({
        "@rust_crates//:std": ["std"],
        "//conditions:default": [""],
    }),
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend",
        ":pw_log_backend_api",
    ],
)

rust_test(
    name = "pw_log_test",
    crate = ":pw_log",
    crate_features = select({
        "@rust_crates//:std": ["std"],
        "//conditions:default": [""],
    }),
    # TODO: b/343726867 - support on-device rust tests
    target_compatible_with = incompatible_with_mcu(),
)

rust_doc_test(
    name = "pw_log_doc_test",
    crate = ":pw_log",
    target_compatible_with = select({
        "@platforms//os:none": ["@platforms//:incompatible"],
        # TODO: https://github.com/bazelbuild/rules_rust/issues/1431 - enable once rules_rust can set features on doc tests
        "@rules_rust//rust/toolchain/channel:nightly": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
)

rust_library(
    name = "pw_log_backend_api",
    srcs = [
        "pw_log_backend_api.rs",
    ],
    visibility = ["//visibility:public"],
)

rust_library(
    name = "pw_log_backend_println",
    srcs = [
        "pw_log_backend_println.rs",
    ],
    crate_name = "pw_log_backend",
    proc_macro_deps = [":pw_log_backend_println_macro"],
    # TODO: b/343467774 - provide no_std backend by default
    tags = ["manual"],
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
    ],
)

rust_proc_macro(
    name = "pw_log_backend_println_macro",
    srcs = [
        "pw_log_backend_println_macro.rs",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        "//pw_format/rust:pw_format",
        "//pw_status/rust:pw_status",
        "@rust_crates//:proc-macro2",
        "@rust_crates//:quote",
        "@rust_crates//:syn",
    ],
)

# Declare two targets.  One named `pw_log_backend_printf` which uses the
# crate name `pw_log_backend` for use with `pw_log`'s facade pattern.  The
# second named `pw_log_backend_printf_docs` with the crate name
# `pw_log_backend_printf` used for generating docs for the crate.
rust_library(
    name = "pw_log_backend_printf",
    srcs = [
        "pw_log_backend_printf/lib.rs",
        "pw_log_backend_printf/varargs.rs",
    ],
    crate_name = "pw_log_backend",
    proc_macro_deps = [":pw_log_backend_printf_macro"],
    # TODO: b/343467774 - provide no_std backend by default
    target_compatible_with = incompatible_with_mcu(),
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        "//pw_bytes/rust:pw_bytes",
        "//pw_format/rust:pw_format_core",
    ],
)

rust_library(
    name = "pw_log_backend_printf_docs",
    srcs = [
        "pw_log_backend_printf/lib.rs",
        "pw_log_backend_printf/varargs.rs",
    ],
    crate_name = "pw_log_backend_printf",
    proc_macro_deps = [":pw_log_backend_printf_macro"],
    # TODO: b/343467774 - provide no_std backend by default
    target_compatible_with = incompatible_with_mcu(),
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        "//pw_bytes/rust:pw_bytes",
        "//pw_format/rust:pw_format_core",
    ],
)

# Use the _docs target for tests and doc tests so they get the unique crate
# name.
rust_test(
    name = "pw_log_backend_printf_test",
    crate = ":pw_log_backend_printf_docs",
    # TODO: b/343726867 - support on-device rust tests
    target_compatible_with = incompatible_with_mcu(),
)

rust_doc_test(
    name = "pw_log_backend_printf_doc_test",
    crate = ":pw_log_backend_printf_docs",
    target_compatible_with = incompatible_with_mcu(),
)

rust_proc_macro(
    name = "pw_log_backend_printf_macro",
    srcs = [
        "pw_log_backend_printf_macro.rs",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        "//pw_format/rust:pw_format",
        "//pw_status/rust:pw_status",
        "@rust_crates//:proc-macro2",
        "@rust_crates//:quote",
        "@rust_crates//:syn",
    ],
)

rust_library(
    name = "printf_backend_test",
    srcs = [
        "backend_tests.rs",
        "printf_backend_test.rs",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        ":pw_log_backend_printf",
        "@rust_crates//:libc",
        "@rust_crates//:nix",
    ],
)

rust_test(
    name = "printf_backend_test_test",
    crate = ":printf_backend_test",
    crate_features = select({
        "@rules_rust//rust/toolchain/channel:nightly": ["nightly"],
        "//conditions:default": [],
    }),
    # TODO: b/343726867 - support on-device rust tests
    target_compatible_with = incompatible_with_mcu(),
)

rust_library(
    name = "println_backend_test",
    srcs = [
        "backend_tests.rs",
        "println_backend_test.rs",
    ],
    target_compatible_with = select({
        # This test needs unstable features.
        "@rules_rust//rust/toolchain/channel:nightly": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    visibility = ["//visibility:public"],
    deps = [
        ":pw_log_backend_api",
        ":pw_log_backend_println",
    ],
)

rust_test(
    name = "println_backend_test_test",
    crate = ":println_backend_test",
    # TODO: b/343726867 - support on-device rust tests
    target_compatible_with = incompatible_with_mcu(),
)

label_flag(
    name = "pw_log_backend",
    build_setting_default = ":pw_log_backend_printf",
    visibility = ["//visibility:public"],
)
